package com.example.service;

import com.example.control.indexController;
import com.example.dao.commentMapper;
import com.example.dao.statisticMapper;
import com.example.domain.t_article;
import com.example.domain.t_comment;
import com.example.domain.t_statistic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class ICommentService {
    private static final Logger logger = LoggerFactory.getLogger(indexController.class);


    @Autowired
    private commentMapper com;

    @Autowired
    private statisticMapper stat;

    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redis;
    /**
     * 根据文章id获取所有评论
     * @param id article_id
     * @return
     */
    public List<t_comment> getAll(int id){
        return com.selectAllById(id);
    }

    /**
     * 封装评论分页
     * @param id article_id
     * @param count 页码
     * @param size 页的大小
     * @return
     */
    public PageInfo<t_comment> getAll(int id,int count,int size){
        PageHelper.startPage(count,size);
        PageInfo<t_comment> infos = new PageInfo<>(getAll(id));
        return infos;
    }
    //发表评论
    public boolean insert(t_comment comment){
        Integer article_id = comment.getArticle_id();
        //修改评论表
        t_statistic statistic = stat.getStatisticById(article_id);
        statistic.setComments_num(statistic.getComments_num()+1);
        stat.updateStatisticByArticleId(statistic);

        try{
            t_article o = (t_article)redis.opsForValue().get("article_" + article_id);
            o.setComments_num(o.getComments_num()+1);
            redis.opsForValue().set("article_"+article_id,o);
        }catch (Exception e){
            logger.error("更新缓存失败，"+e.getMessage());
        }
        return com.insertComment(comment);
    }
    //删除评论
    public boolean delete(int id){
        Integer article_id = com.selectById(id).getArticle_id();
        //修改评论表
        t_statistic statistic = stat.getStatisticById(article_id);
        statistic.setComments_num(statistic.getComments_num()-1);
        stat.updateStatisticByArticleId(statistic);
        try{
            t_article o = (t_article)redis.opsForValue().get("article_" + article_id);
            o.setComments_num(o.getComments_num()-1);
            redis.opsForValue().set("article_"+article_id,o);
        }catch (Exception e){
            logger.error("更新缓存失败，"+e.getMessage());
        }
        return com.deleteCommentById(id);
    }

    //获取某一 评论
    public t_comment get(Integer c_id) {
        return com.selectById(c_id);
    }

    //修改评论作者名字
    public void setAuther(String username, String Newusername) {
        if(Newusername==null||Newusername.equals("")||Newusername.equals(username))return;
        com.UpdateAuther(username,Newusername);
    }
}
